package leetcode.leetcode100;

public class trap {
    public static void main(String[] args) {
        int[] height = {5,5,1,7,1,1,5,2,7,6};
        System.out.println(process(height));
    }
    public static int process(int[] height){
        int p1 =0;
        int p2 = height.length-1;
        while (p1+1<height.length && height[p1]<height[p1+1]){
            p1++;
        }
        while (p2-1>0 && height[p2]<height[p2-1]){
            p2--;
        }
        int count = 0;


        while (p1!=p2){

            if (height[p1]<=height[p2]){
                int cur = 0;
                while (cur+p1<height.length-1 && height[p1]>=height[cur+p1] && p1+cur!=p2){
                    count = count + height[p1] - height[cur+p1];
                    cur++;
                }
                p1 = p1+cur;
            }else {
                int cur2 = 0;
                while (p2 - cur2!=p1 && height[p2]>=height[p2-cur2] && p1!=p2){
                    count = count + height[p2] - height[p2-cur2];
                    cur2++;
                }
                p2 = p2 - cur2;
            }
        }
        return count;
    }
}
